#!/usr/bin/env python

#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
preprocess the raw data before xml processing
"""

import re

def preprocess(raw):
	raw = fixEscaped(raw)
	return raw

def fixApos(raw):
	apos = re.compile('&#xB4;')
	return apos.sub('\'',raw)

def fixEscaped(raw):
	for k,v in escaped.iteritems():
		# find escaped with no beginning &
		r = re.compile('&amp;' + k + ';')
		raw = r.sub(v,raw)
	return raw


escaped = {
	'quot':'&#34;',
	'amp':'&#38;',
	'apos':'&#39;',
	'lt':'&#60;',
	'gt':'&#62;',
	'Agrave':'&#192;',
	'Aacute':'&#193;',
	'Acirc':'&#194;',
	'Atilde':'&#195;',
	'Auml':'&#196;',
	'Aring':'&#197;',
	'Ccedil':'&#199;',
	'Egrave':'&#200;',
	'Eacute':'&#201;',
	'Ecirc':'&#202;',
	'Euml':'&#203;',
	'Igrave':'&#204;',
	'Iacute':'&#205;',
	'Icirc':'&#206;',
	'Iuml':'&#207;',
	'Ntilde':'&#209;',
	'Ograve':'&#210;',
	'Oacute':'&#211;',
	'Ocirc':'&#212;',
	'Otilde':'&#213;',
	'Ouml':'&#214;',
	'Oslash':'&#216;',
	'Ugrave':'&#217;',
	'Uacute':'&#218;',
	'Ucirc':'&#219;',
	'Uuml':'&#220;',
	'Yacute':'&#221;',
	'agrave':'&#224;',
	'aacute':'&#225;',
	'acirc':'&#226;',
	'atilde':'&#227;',
	'auml':'&#228;',
	'aring':'&#229;',
	'ccedil':'&#231;',
	'egrave':'&#232;',
	'eacute':'&#233;',
	'ecirc':'&#234;',
	'euml':'&#235;',
	'igrave':'&#236;',
	'iacute':'&#237;',
	'icirc':'&#238;',
	'iuml':'&#239;',
	'ntilde':'&#241;',
	'ograve':'&#242;',
	'oacute':'&#243;',
	'ocirc':'&#244;',
	'otilde':'&#245;',
	'ouml':'&#246;',
	'oslash':'&#248;',
	'ugrave':'&#249;',
	'uacute':'&#250;',
	'ucirc':'&#251;',
	'uuml':'&#252;',
	'yacute':'&#253;',
	'yuml':'&#255;',
	}
# eof

